home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-includes / links.php < prev    next >
Encoding:
PHP Script  |  2005-04-01  |  20.1 KB  |  568 lines

  1. <?php
  2.  
  3. /** function get_linksbyname()
  4.  ** Gets the links associated with category 'cat_name'.
  5.  ** Parameters:
  6.  **   cat_name (default 'noname')  - The category name to use. If no
  7.  **     match is found uses all
  8.  **   before (default '')  - the html to output before the link
  9.  **   after (default '<br />')  - the html to output after the link
  10.  **   between (default ' ')  - the html to output between the link/image
  11.  **     and it's description. Not used if no image or show_images == true
  12.  **   show_images (default true) - whether to show images (if defined).
  13.  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
  14.  **     'url', 'description' or 'rating'. Or maybe owner. If you start the
  15.  **     name with an underscore the order will be reversed.
  16.  **     You can also specify 'rand' as the order which will return links in a
  17.  **     random order.
  18.  **   show_description (default true) - whether to show the description if
  19.  **     show_images=false/not defined
  20.  **   show_rating (default false) - show rating stars/chars
  21.  **   limit (default -1) - Limit to X entries. If not specified, all entries
  22.  **     are shown.
  23.  **   show_updated (default 0) - whether to show last updated timestamp
  24.  */
  25. function get_linksbyname($cat_name = "noname", $before = '', $after = '<br />',
  26.                          $between = " ", $show_images = true, $orderby = 'id',
  27.                          $show_description = true, $show_rating = false,
  28.                          $limit = -1, $show_updated = 0) {
  29.     global $wpdb;
  30.     $cat_id = -1;
  31.     $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
  32.     if ($results) {
  33.         foreach ($results as $result) {
  34.             $cat_id = $result->cat_id;
  35.         }
  36.     }
  37.     get_links($cat_id, $before, $after, $between, $show_images, $orderby,
  38.               $show_description, $show_rating, $limit, $show_updated);
  39. }
  40.  
  41. function bool_from_yn($yn) {
  42.     if ($yn == 'Y') return 1;
  43.     return 0;
  44. }
  45.  
  46. /** function wp_get_linksbyname()
  47.  ** Gets the links associated with the named category.
  48.  ** Parameters:
  49.  **   category (no default)  - The category to use.
  50.  **/
  51. function wp_get_linksbyname($category, $args = '') {
  52.     global $wpdb;
  53.  
  54.     $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
  55.                                                 . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
  56.                                                 . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_name='$category'");
  57.  
  58.     if (! $cat) {
  59.         return;
  60.     }
  61.  
  62.     if (empty($args)) {
  63.         if ($cat->sort_desc == 'Y') {
  64.             $cat->sort_order = '_'.$cat->sort_order;
  65.         }
  66.         get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
  67.                             $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
  68.                             bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
  69.                             $cat->list_limit, bool_from_yn($cat->show_updated));
  70.     } else {
  71.         $args = add_query_arg('category', $cat->cat_id, $args);
  72.         wp_get_links($args);
  73.     }
  74. } // end wp_get_linksbyname
  75.  
  76. /** function wp_get_links()
  77.  ** Gets the links associated with category n.
  78.  ** Parameters:
  79.  **   category (no default)  - The category to use.
  80.  ** or:
  81.  **   a query string
  82.  **/
  83. function wp_get_links($args = '') {
  84.     global $wpdb;
  85.  
  86.     if (!empty($args) && false === strpos($args, '=')) {
  87.         // If args is not a query string, it's a category id.
  88.         $category = $args;
  89.         $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
  90.                                                     . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
  91.                                                     . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$category");
  92.         if ($cat) {
  93.             if ($cat->sort_desc == 'Y') {
  94.                 $cat->sort_order = '_'.$cat->sort_order;
  95.             }
  96.             get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
  97.                                 $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
  98.                                 bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
  99.                                 $cat->list_limit, bool_from_yn($cat->show_updated));
  100.         }
  101.     } else {
  102.         parse_str($args);
  103.  
  104.         if (! isset($category))    $category = -1;
  105.         if (! isset($before)) $before = '';
  106.         if (! isset($after)) $after = '<br />';
  107.         if (! isset($between))    $between = ' ';
  108.         if (! isset($show_images)) $show_images = true;
  109.         if (! isset($orderby)) $orderby = 'name';
  110.         if (! isset($show_description)) $show_description = true;
  111.         if (! isset($show_rating)) $show_rating = false;
  112.         if (! isset($limit)) $limit = -1;
  113.         if (! isset($show_updated)) $show_updated = 1;
  114.         if (! isset($echo)) $echo = true;
  115.  
  116.         get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
  117.     }
  118. } // end wp_get_links
  119.  
  120. /** function get_links()
  121.  ** Gets the links associated with category n.
  122.  ** Parameters:
  123.  **   category (default -1)  - The category to use. If no category supplied
  124.  **      uses all
  125.  **   before (default '')  - the html to output before the link
  126.  **   after (default '<br />')  - the html to output after the link
  127.  **   between (default ' ')  - the html to output between the link/image
  128.  **     and it's description. Not used if no image or show_images == true
  129.  **   show_images (default true) - whether to show images (if defined).
  130.  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
  131.  **     'url', 'description', or 'rating'. Or maybe owner. If you start the
  132.  **     name with an underscore the order will be reversed.
  133.  **     You can also specify 'rand' as the order which will return links in a
  134.  **     random order.
  135.  **   show_description (default true) - whether to show the description if
  136.  **    show_images=false/not defined .
  137.  **   show_rating (default false) - show rating stars/chars
  138.  **   limit (default -1) - Limit to X entries. If not specified, all entries
  139.  **     are shown.
  140.  **   show_updated (default 0) - whether to show last updated timestamp
  141.  */
  142. function get_links($category = -1, $before = '', $after = '<br />',
  143.                    $between = ' ', $show_images = true, $orderby = 'name',
  144.                    $show_description = true, $show_rating = false,
  145.                    $limit = -1, $show_updated = 1, $echo = true) {
  146.  
  147.     global $wpdb;
  148.  
  149.     $direction = ' ASC';
  150.     $category_query = "";
  151.     if ($category != -1) {
  152.         $category_query = " AND link_category = $category ";
  153.     }
  154.     if (get_settings('links_recently_updated_time')) {
  155.         $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL ".get_settings('links_recently_updated_time')." MINUTE) >= NOW(), 1,0) as recently_updated ";
  156.     } else {
  157.         $recently_updated_test = '';
  158.     }
  159.     if ($show_updated) {
  160.         $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
  161.     }
  162.  
  163.     $orderby=strtolower($orderby);
  164.     if ($orderby == '')
  165.         $orderby = 'id';
  166.     if (substr($orderby,0,1) == '_') {
  167.         $direction = ' DESC';
  168.         $orderby = substr($orderby,1);
  169.     }
  170.  
  171.     switch($orderby) {
  172.         case 'length':
  173.         $length = ",CHAR_LENGTH(link_name) AS length";
  174.         break;
  175.         case 'rand':
  176.             $orderby = 'rand()';
  177.             break;
  178.         default:
  179.             $orderby = " link_" . $orderby;
  180.     }
  181.  
  182.     if (!isset($length)) {
  183.         $length = "";
  184.     }
  185.  
  186.     $sql = "SELECT link_url, link_name, link_image, link_target,
  187.             link_description, link_rating, link_rel $length $recently_updated_test $get_updated
  188.             FROM $wpdb->links
  189.             WHERE link_visible = 'Y' " .
  190.            $category_query;
  191.     $sql .= ' ORDER BY ' . $orderby;
  192.     $sql .= $direction;
  193.     /* The next 2 lines implement LIMIT TO processing */
  194.     if ($limit != -1)
  195.         $sql .= " LIMIT $limit";
  196.     //echo $sql;
  197.     $results = $wpdb->get_results($sql);
  198.     if (!$results) {
  199.         return;
  200.     }
  201.     
  202.     $output = "";
  203.     
  204.     foreach ($results as $row) {
  205.         if (!isset($row->recently_updated)) $row->recently_updated = false;
  206.         $output .= ($before);
  207.         
  208.         if ($show_updated && $row->recently_updated) {
  209.             $output .= get_settings('links_recently_updated_prepend');
  210.         }
  211.         
  212.         $the_link = '#';
  213.         
  214.         if ( !empty($row->link_url) )
  215.             $the_link = wp_specialchars($row->link_url);
  216.         $rel = $row->link_rel;
  217.         
  218.         if ($rel != '') {
  219.             $rel = " rel='$rel'";
  220.         }
  221.         
  222.         $desc = wp_specialchars($row->link_description, ENT_QUOTES);
  223.         $name = wp_specialchars($row->link_name, ENT_QUOTES);
  224.  
  225.         $title = $desc;
  226.  
  227.         if ($show_updated) {
  228.            if (substr($row->link_updated_f,0,2) != '00') {
  229.                 $title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) .')';
  230.             }
  231.         }
  232.  
  233.         if ('' != $title) {
  234.             $title = " title='$title'";
  235.         }
  236.  
  237.         $alt = " alt='$name'";
  238.             
  239.         $target = $row->link_target;
  240.         if ('' != $target) {
  241.             $target = " target='$target'";
  242.         }
  243.         
  244.         $output.= "<a href='$the_link'";
  245.         $output.= $rel . $title . $target;
  246.         $output.= '>';
  247.         
  248.         if (($row->link_image != null) && $show_images) {
  249.             if (strstr($row->link_image, 'http'))
  250.                 $output.= "<img src='$row->link_image' $alt $title />";
  251.             else // If it's a relative path
  252.                 $output.= "<img src='" . get_settings('siteurl') . "$row->link_image' $alt $title />";
  253.         } else {
  254.             $output.= $name;
  255.         }
  256.         
  257.         $output.= '</a>';
  258.         
  259.         if ($show_updated && $row->recently_updated) {
  260.             $output.= get_settings('links_recently_updated_append');
  261.         }
  262.  
  263.         if ($show_description && ($desc != '')) {
  264.             $output.= $between.$desc;
  265.         }
  266.         $output.= "$after\n";
  267.     } // end while
  268.     
  269.     if($echo) {
  270.         echo $output;
  271.     } else {
  272.         return $output;
  273.     }
  274. }
  275.  
  276.  
  277. /** function get_linkobjectsbyname()
  278.  ** Gets an array of link objects associated with category 'cat_name'.
  279.  ** Parameters:
  280.  **   cat_name (default 'noname')  - The category name to use. If no
  281.  **     match is found uses all
  282.  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
  283.  **     'url', 'description', or 'rating'. Or maybe owner. If you start the
  284.  **     name with an underscore the order will be reversed.
  285.  **     You can also specify 'rand' as the order which will return links in a
  286.  **     random order.
  287.  **   limit (default -1) - Limit to X entries. If not specified, all entries
  288.  **     are shown.
  289.  **
  290.  ** Use this like:
  291.  ** $links = get_linkobjectsbyname('fred');
  292.  ** foreach ($links as $link) {
  293.  **   echo '<li>'.$link->link_name.'</li>';
  294.  ** }
  295.  **/
  296. function get_linkobjectsbyname($cat_name = "noname" , $orderby = 'name', $limit = -1) {
  297.     global $wpdb;
  298.     $cat_id = -1;
  299.     $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
  300.     if ($results) {
  301.         foreach ($results as $result) {
  302.             $cat_id = $result->cat_id;
  303.         }
  304.     }
  305.     return get_linkobjects($cat_id, $orderby, $limit);
  306. }
  307.  
  308. /** function get_linkobjects()
  309.  ** Gets an array of link objects associated with category n.
  310.  ** Parameters:
  311.  **   category (default -1)  - The category to use. If no category supplied
  312.  **      uses all
  313.  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
  314.  **     'url', 'description', or 'rating'. Or maybe owner. If you start the
  315.  **     name with an underscore the order will be reversed.
  316.  **     You can also specify 'rand' as the order which will return links in a
  317.  **     random order.
  318.  **   limit (default -1) - Limit to X entries. If not specified, all entries
  319.  **     are shown.
  320.  **
  321.  ** Use this like:
  322.  ** $links = get_linkobjects(1);
  323.  ** if ($links) {
  324.  **   foreach ($links as $link) {
  325.  **     echo '<li>'.$link->link_name.'<br />'.$link->link_description.'</li>';
  326.  **   }
  327.  ** }
  328.  ** Fields are:
  329.  ** link_id
  330.  ** link_url
  331.  ** link_name
  332.  ** link_image
  333.  ** link_target
  334.  ** link_category
  335.  ** link_description
  336.  ** link_visible
  337.  ** link_owner
  338.  ** link_rating
  339.  ** link_updated
  340.  ** link_rel
  341.  ** link_notes
  342.  **/
  343. function get_linkobjects($category = -1, $orderby = 'name', $limit = -1) {
  344.     global $wpdb;
  345.  
  346.     $sql = "SELECT * FROM $wpdb->links WHERE link_visible = 'Y'";
  347.     if ($category != -1) {
  348.         $sql .= " AND link_category = $category ";
  349.     }
  350.     if ($orderby == '')
  351.         $orderby = 'id';
  352.     if (substr($orderby,0,1) == '_') {
  353.         $direction = ' DESC';
  354.         $orderby = substr($orderby,1);
  355.     }
  356.     if (strcasecmp('rand',$orderby) == 0) {
  357.         $orderby = 'rand()';
  358.     } else {
  359.         $orderby = " link_" . $orderby;
  360.     }
  361.     $sql .= ' ORDER BY ' . $orderby;
  362.     $sql .= $direction;
  363.     /* The next 2 lines implement LIMIT TO processing */
  364.     if ($limit != -1)
  365.         $sql .= " LIMIT $limit";
  366.  
  367.     $results = $wpdb->get_results($sql);
  368.     if ($results) {
  369.         foreach ($results as $result) {
  370.             $result->link_url         = $result->link_url;
  371.             $result->link_name        = $result->link_name;
  372.             $result->link_description = $result->link_description;
  373.             $result->link_notes       = $result->link_notes;
  374.             $newresults[] = $result;
  375.         }
  376.     }
  377.     return $newresults;
  378. }
  379.  
  380. function get_linkrating($link) {
  381.     return apply_filters('link_rating', $link->link_rating);
  382. }
  383.  
  384.  
  385. /** function get_linksbyname_withrating()
  386.  ** Gets the links associated with category 'cat_name' and display rating stars/chars.
  387.  ** Parameters:
  388.  **   cat_name (default 'noname')  - The category name to use. If no
  389.  **     match is found uses all
  390.  **   before (default '')  - the html to output before the link
  391.  **   after (default '<br />')  - the html to output after the link
  392.  **   between (default ' ')  - the html to output between the link/image
  393.  **     and it's description. Not used if no image or show_images == true
  394.  **   show_images (default true) - whether to show images (if defined).
  395.  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
  396.  **     'url' or 'description'. Or maybe owner. If you start the
  397.  **     name with an underscore the order will be reversed.
  398.  **     You can also specify 'rand' as the order which will return links in a
  399.  **     random order.
  400.  **   show_description (default true) - whether to show the description if
  401.  **     show_images=false/not defined
  402.  **   limit (default -1) - Limit to X entries. If not specified, all entries
  403.  **     are shown.
  404.  **   show_updated (default 0) - whether to show last updated timestamp
  405.  */
  406. function get_linksbyname_withrating($cat_name = "noname", $before = '',
  407.                                     $after = '<br />', $between = " ",
  408.                                     $show_images = true, $orderby = 'id',
  409.                                     $show_description = true, $limit = -1, $show_updated = 0) {
  410.  
  411.     get_linksbyname($cat_name, $before, $after, $between, $show_images,
  412.                     $orderby, $show_description, true, $limit, $show_updated);
  413. }
  414.  
  415. /** function get_links_withrating()
  416.  ** Gets the links associated with category n and display rating stars/chars.
  417.  ** Parameters:
  418.  **   category (default -1)  - The category to use. If no category supplied
  419.  **      uses all
  420.  **   before (default '')  - the html to output before the link
  421.  **   after (default '<br />')  - the html to output after the link
  422.  **   between (default ' ')  - the html to output between the link/image
  423.  **     and it's description. Not used if no image or show_images == true
  424.  **   show_images (default true) - whether to show images (if defined).
  425.  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
  426.  **     'url' or 'description'. Or maybe owner. If you start the
  427.  **     name with an underscore the order will be reversed.
  428.  **     You can also specify 'rand' as the order which will return links in a
  429.  **     random order.
  430.  **   show_description (default true) - whether to show the description if
  431.  **    show_images=false/not defined .
  432.  **   limit (default -1) - Limit to X entries. If not specified, all entries
  433.  **     are shown.
  434.  **   show_updated (default 0) - whether to show last updated timestamp
  435.  */
  436. function get_links_withrating($category = -1, $before = '', $after = '<br />',
  437.                               $between = " ", $show_images = true,
  438.                               $orderby = 'id', $show_description = true,
  439.                               $limit = -1, $show_updated = 0) {
  440.  
  441.     get_links($category, $before, $after, $between, $show_images, $orderby,
  442.               $show_description, true, $limit, $show_updated);
  443. }
  444.  
  445. /** function get_linkcatname()
  446.  ** Gets the name of category n.
  447.  ** Parameters: id (default 0)  - The category to get. If no category supplied
  448.  **                uses 0
  449.  */
  450. function get_linkcatname($id = 0) {
  451.     global $wpdb;
  452.     $cat_name = '';
  453.     if ('' != $id) {
  454.         $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->linkcategories WHERE cat_id=$id");
  455.     }
  456.     return $cat_name;
  457. }
  458.  
  459. /** function get_get_autotoggle()
  460.  ** Gets the auto_toggle setting of category n.
  461.  ** Parameters: id (default 0)  - The category to get. If no category supplied
  462.  **                uses 0
  463.  */
  464. function get_autotoggle($id = 0) {
  465.     global $wpdb;
  466.     $auto_toggle = $wpdb->get_var("SELECT auto_toggle FROM $wpdb->linkcategories WHERE cat_id=$id");
  467.     if ('' == $auto_toggle)
  468.         $auto_toggle = 'N';
  469.     return $auto_toggle;
  470. }
  471.  
  472. /** function links_popup_script()
  473.  ** This function contributed by Fullo -- http://sprite.csr.unibo.it/fullo/
  474.  ** Show the link to the links popup and the number of links
  475.  ** Parameters:
  476.  **   text (default Links)  - the text of the link
  477.  **   width (default 400)  - the width of the popup window
  478.  **   height (default 400)  - the height of the popup window
  479.  **   file (default linkspopup.php) - the page to open in the popup window
  480.  **   count (default true) - the number of links in the db
  481.  */
  482. function links_popup_script($text = 'Links', $width=400, $height=400,
  483.                             $file='links.all.php', $count = true) {
  484.    if ($count == true) {
  485.       $counts = $wpdb->get_var("SELECT count(*) FROM $wpdb->links");
  486.    }
  487.  
  488.    $javascript = "<a href=\"#\" " .
  489.                  " onclick=\"javascript:window.open('$file?popup=1', '_blank', " .
  490.                  "'width=$width,height=$height,scrollbars=yes,status=no'); " .
  491.                  " return false\">";
  492.    $javascript .= $text;
  493.  
  494.    if ($count == true) {
  495.       $javascript .= " ($counts)";
  496.    }
  497.  
  498.    $javascript .="</a>\n\n";
  499.    echo $javascript;
  500. }
  501.  
  502.  
  503. /*
  504.  * function get_links_list()
  505.  *
  506.  * added by Dougal
  507.  *
  508.  * Output a list of all links, listed by category, using the
  509.  * settings in $wpdb->linkcategories and output it as a nested
  510.  * HTML unordered list.
  511.  *
  512.  * Parameters:
  513.  *   order (default 'name')  - Sort link categories by 'name' or 'id'
  514.  *   hide_if_empty (default true)  - Supress listing empty link categories
  515.  */
  516. function get_links_list($order = 'name', $hide_if_empty = 'obsolete') {
  517.     global $wpdb;
  518.  
  519.     $order = strtolower($order);
  520.  
  521.     // Handle link category sorting
  522.     if (substr($order,0,1) == '_') {
  523.         $direction = ' DESC';
  524.         $order = substr($order,1);
  525.     }
  526.  
  527.     // if 'name' wasn't specified, assume 'id':
  528.     $cat_order = ('name' == $order) ? 'cat_name' : 'cat_id';
  529.  
  530.     if (!isset($direction)) $direction = '';
  531.     // Fetch the link category data as an array of hashesa
  532.     $cats = $wpdb->get_results("
  533.         SELECT DISTINCT link_category, cat_name, show_images, 
  534.             show_description, show_rating, show_updated, sort_order, 
  535.             sort_desc, list_limit
  536.         FROM `$wpdb->links` 
  537.         LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
  538.         WHERE link_visible =  'Y'
  539.             AND list_limit <> 0
  540.         ORDER BY $cat_order $direction ", ARRAY_A);
  541.  
  542.     // Display each category
  543.     if ($cats) {
  544.         foreach ($cats as $cat) {
  545.             // Handle each category.
  546.             // First, fix the sort_order info
  547.             $orderby = $cat['sort_order'];
  548.             $orderby = (bool_from_yn($cat['sort_desc'])?'_':'') . $orderby;
  549.  
  550.             // Display the category name
  551.             echo '    <li id="linkcat-' . $cat['link_category'] . '"><h2>' . $cat['cat_name'] . "</h2>\n\t<ul>\n";
  552.             // Call get_links() with all the appropriate params
  553.             get_links($cat['link_category'],
  554.                 '<li>',"</li>","\n",
  555.                 bool_from_yn($cat['show_images']),
  556.                 $orderby,
  557.                 bool_from_yn($cat['show_description']),
  558.                 bool_from_yn($cat['show_rating']),
  559.                 $cat['list_limit'],
  560.                 bool_from_yn($cat['show_updated']));
  561.  
  562.             // Close the last category
  563.             echo "\n\t</ul>\n</li>\n";
  564.         }
  565.     }
  566. }
  567.  
  568. ?>